package com.cheli.managerAsset.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheli.commonutils.R;
import com.cheli.managerAsset.entity.AsAsset;
import com.cheli.managerAsset.entity.AssetStaff;
import com.cheli.managerAsset.entity.vo.AssetQuery;
import com.cheli.managerAsset.entity.vo.AssetStaffBackVo;
import com.cheli.managerAsset.service.AsAssetService;
import com.cheli.managerAsset.service.AssetStaffService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname AssetStaffController
 * @Description 资产借出归还
 * @Date 2021/5/6 23:10
 * @Created by cl
 */
@RestController
@RequestMapping("/asManager/assetstaff")
@Api(value = "category", tags = "资产分类管理")
@CrossOrigin
@Slf4j
public class AssetStaffController {

    @Autowired
    private AssetStaffService assetStaffService;

    @Autowired
    private AsAssetService asAssetService;

    @GetMapping("pageAssetStaff/{current}/{limit}")
    public R pageSectionCondition(@PathVariable Long current,
                                  @PathVariable Long limit){
        log.info("借出归还分页条件查询");
       List<AssetStaffBackVo> backAssetList = assetStaffService.getBackAssetList(null);
        int total = backAssetList.size();
        return R.ok().data("total", total).data("rows", backAssetList);
    }

    @GetMapping("pageAssetStaff")
    public R pageAssetStaff(){
        log.info("借出归还分页管理员查询");
       List<AssetStaffBackVo> assetStaffList = assetStaffService.getAssetStaffList();
        return R.ok().data("rows", assetStaffList);
    }

    @GetMapping("over/{id}")
    public R overAssetStaff(@PathVariable String id){
        log.info("通过借用或归还");
       AssetStaff assetStaff = assetStaffService.getById(id);
       if("eding".equals(assetStaff.getStatus())){
            assetStaff.setStatus("ed");
            AsAsset asset = asAssetService.getById(assetStaff.getAssetId());
            asset.setIsOut(0);
            asAssetService.updateById(asset);
        }else {
           assetStaff.setStatus("my");
        }
        assetStaffService.updateById(assetStaff);
        return R.ok();
    }

    @GetMapping("back/{id}")
    public R backAssetStaff(@PathVariable String id){
        log.info("驳回借用或归还");
       AssetStaff assetStaff = assetStaffService.getById(id);
       if("eding".equals(assetStaff.getStatus())) {
            assetStaff.setStatus("my");
        }else{
           assetStaff.setStatus("back");
            AsAsset asset = asAssetService.getById(assetStaff.getAssetId());
            asset.setIsOut(0);
        }
        assetStaffService.updateById(assetStaff);
        return R.ok();
    }
}
